package com.UdpRecieve.framework.config;

import com.UdpRecieve.tool.Global;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
public class RedisConfig {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    private static String host = Global.getRedisHost();

    private static String port = Global.getRedisPort();

    private static Integer maxIdle = Global.getRedisMaxIdle();

    private static Integer maxActive = Global.getRedisMaxActive();

    private static Integer maxAWait = Global.getRedisMaxWait();

    private static Integer timeout = Global.getRedisTimeout();

    private static String password = Global.getRedisPassword();


    /**
     * jedis配置
     *
     * @return
     */
    public JedisPoolConfig getJedisPoolConfig() {
        logger.info("初始化jedis");
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle); //最大空闲数
        config.setMaxWaitMillis(maxAWait); //最大等待时间
        config.setMaxTotal(maxActive); //最大连接数
        return config;
    }

    /**
     * 获取jedispool
     *
     * @return
     */
    @Bean
    public JedisPool getJedisPool() {
        logger.info("获取jedis连接池");
        JedisPoolConfig config = getJedisPoolConfig();
        JedisPool jedisPool = null;
        if (this.password.isEmpty() || this.password == "null") {
            jedisPool = new JedisPool(
                    config,
                    host,
                    Integer.valueOf(port),
                    this.timeout);
        } else {
            jedisPool = new JedisPool(
                    config,
                    host,
                    Integer.valueOf(port),
                    this.timeout,
                    this.password);
        }
        return jedisPool;
    }

}
